from django.db import models
from common.models import BaseModel


class DateTimeFieldFormat(models.DateTimeField):
    """
    数据库datetime类型字段格式化(%Y-%m-%d %H:%M:%S)
    precision:需要保留的小数位数
    """
    def __init__(self, verbose_name=None, name=None, precision=0, **kwargs):
        self.precision = precision
        super().__init__(verbose_name, name, **kwargs)

    def db_type(self, connection):
        return 'datetime(%d)' % self.precision


# 设备信息
class deviceInfo(BaseModel):
    code = models.CharField(max_length=32, null=True, blank=True, verbose_name="设备编码")
    name = models.CharField(max_length=32, null=True, blank=True, verbose_name="设备名称")
    ip_address = models.CharField(max_length=32, null=True, blank=True, verbose_name="设备IP地址")
    port = models.CharField(max_length=32, null=True, blank=True, verbose_name="设备端口号")
    desc = models.CharField(max_length=64, null=True, blank=True, verbose_name="设备描述")

    def __str__(self):
        return "{}".format(self.name)

    class Meta:
        verbose_name = "1.1,设备信息"
        verbose_name_plural = "1.1,设备信息"

# 通道信息
class channelInfo(BaseModel):
    belong_device = models.ForeignKey(deviceInfo,null=True,blank=True,on_delete=models.SET_NULL,verbose_name="所属设备",related_name="channel_belong_device",)
    code = models.CharField(max_length=32, null=True, blank=True, verbose_name="通道编码")
    name = models.CharField(max_length=32, null=True, blank=True, verbose_name="通道名称")
    meter_start = models.CharField(max_length=32, null=True, blank=True, verbose_name="起始位置")
    meter_end = models.CharField(max_length=32, null=True, blank=True, verbose_name="结束位置")
    total_meter = models.CharField(max_length=32, null=True, blank=True, verbose_name="总长度")
    temp_compensate = models.CharField(max_length=32, null=True, blank=True, verbose_name="温度补偿值")
    fiber_refracty = models.CharField(max_length=32, null=True, blank=True, verbose_name="光纤折射率")
    fiber_coefficient = models.CharField(max_length=32, null=True, blank=True, verbose_name="光纤衰减系数")
    is_warning_play = models.BooleanField(default=False, verbose_name="是否播放报警信息")
    desc = models.CharField(max_length=64, null=True, blank=True, verbose_name="通道描述")


    def __str__(self):
        return "{}".format(self.name)

    class Meta:
        verbose_name = "1.2,通道信息"
        verbose_name_plural = "1.2,通道信息"

# 实时温度信息
class temperatureInfo(BaseModel):
    belong_channel = models.ForeignKey(channelInfo,null=True,blank=True,on_delete=models.SET_NULL,verbose_name="所属通道",related_name="temperature_belong_channel",)
    meter_address = models.DecimalField(
        max_digits=10, decimal_places=1, null=True, blank=True, verbose_name="测量地址"
    )
    temperature_info = models.DecimalField(
        max_digits=10, decimal_places=2, null=True, blank=True, verbose_name="测量温度"
    )

    order_time = DateTimeFieldFormat( auto_now_add=False, null=True, blank=True, verbose_name="测量时间")

    desc = models.CharField(max_length=64, null=True, blank=True, verbose_name="描述")

    def __str__(self):
        return "{} - {}".format(self.belong_channel.name, self.meter_address)

    def channel_name(self):
        return self.belong_channel.name

    class Meta:
        verbose_name = "1.3,实时温度信息"
        verbose_name_plural = "1.3,实时温度信息"

# 告警分区设置
class warningDistrictSetting(BaseModel):

    code = models.CharField(max_length=32, null=True, blank=True, verbose_name="分区编码")
    name = models.CharField(max_length=32, null=True, blank=True, verbose_name="分区名称")
    meter_start = models.CharField(max_length=32, null=True, blank=True, verbose_name="分区起点")
    meter_end = models.CharField(max_length=32, null=True, blank=True, verbose_name="分区终点")
    temperature_info = models.CharField(max_length=32, null=True, blank=True, verbose_name="报警阀值")
    desc = models.CharField(max_length=64, null=True, blank=True, verbose_name="分区描述")

    def __str__(self):
        return "{}".format(self.name)

    class Meta:
        verbose_name = "1.4,告警分区设置"
        verbose_name_plural = "1.4,告警分区设置"

# 告警设置
class warningSettingInfo(BaseModel):

    DEVICE_TYPE= (
        ("device", "设备"),
        ("pidai", "皮带"),
        ("people", "人员"),
    )

    code = models.CharField(max_length=32, null=True, blank=True, verbose_name="告警编码")
    name = models.CharField(max_length=32, null=True, blank=True, verbose_name="告警名称")
    belong_channel = models.ForeignKey(channelInfo,null=True,blank=True,on_delete=models.SET_NULL,verbose_name="所属通道",related_name="warning_info_belong_channel",)
    meter_start = models.CharField(max_length=32, null=True, blank=True, verbose_name="报警起始位置")
    meter_end = models.CharField(max_length=32, null=True, blank=True, verbose_name="报警结束位置")
    temperature_info = models.CharField(max_length=32, null=True, blank=True, verbose_name="测量数值")

    constant_threshold = models.CharField(max_length=32, null=True, blank=True, verbose_name="定温报警阀值")
    is_constant_threshold = models.BooleanField(default=False, verbose_name="定温报警使能")

    different_threshold = models.CharField(max_length=32, null=True, blank=True, verbose_name="差温报警阀值")
    is_different_threshold = models.BooleanField(default=False, verbose_name="差温报警使能")

    devict_type = models.CharField(
        max_length=16, choices=DEVICE_TYPE, default="device", verbose_name="设备类型"
    )
    device_info = models.CharField(max_length=64, null=True, blank=True, verbose_name="设备信息")

    all_district_details = models.ManyToManyField(
        warningDistrictSetting,
        blank=True,
        related_name="belong_warning_district_details",
        verbose_name="告警分区设置详情",
    )

    desc = models.CharField(max_length=64, null=True, blank=True, verbose_name="描述")

    def __str__(self):
        return "{}".format(self.name)

    class Meta:
        verbose_name = "1.5,告警设置信息"
        verbose_name_plural = "1.5,告警设置信息"




# 告警信息
class warningDeviceInfo(BaseModel):
    WARNING_TYPE = (
        ("constant", "定温报警"),
        ("different", "差温报警"),
        ("offline", "断纤报警"),
    )

    belong_channel = models.ForeignKey(channelInfo,null=True,blank=True,on_delete=models.SET_NULL,verbose_name="所属通道",related_name="warning_device_belong_channel",)
    meter_address = models.CharField(max_length=32, null=True, blank=True, verbose_name="测量地址")
    temperature_info = models.CharField(max_length=32, null=True, blank=True, verbose_name="测量温度")

    order_time = DateTimeFieldFormat( auto_now_add=False, null=True, blank=True, verbose_name="告警时间")

    warning_type = models.CharField(
        max_length=16, choices=WARNING_TYPE, default="constant", verbose_name="告警类型"
    )
    desc = models.CharField(max_length=64, null=True, blank=True, verbose_name="告警描述")


    def __str__(self):
        return "{} - {}".format(self.meter_address, self.temperature_info)

    class Meta:
        verbose_name = "1.6,设备告警信息"
        verbose_name_plural = "1.6,设备告警信息"